/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package l1j.taki;

import java.util.ArrayList;
import java.util.Random;

import l1j.server.server.ActionCodes;
import l1j.server.server.GeneralThreadPool;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_BlueMessage;
import l1j.server.server.serverpackets.S_SystemMessage;

public class DragonCave {

	public final ArrayList<L1PcInstance> _members = new ArrayList<L1PcInstance>();

	private static DragonCave _instance;
	
	private Random _random = new Random();

	public static DragonCave getInstance() {
		if (_instance == null) {
			_instance = new DragonCave();
		}
		return _instance;
	}
	
	private static final int _readytime = 30 * 1000; //等待時間
	private static final int _cleartime = 10 * 1000; //清潔時間
	private static final int _speaktime = 5 * 1000;
	private static final int _bosstime = 1 * 60 * 1000;
	
	private int _ramdrgon[] = {905121, 905122, 905123, 905124};  //風地水火
	
	private static final int _bossId2 = 905129; //地
	private static final int _bossId3 = 905130; //火
	private static final int _bossId4 = 905131; //水
	private static final int _bossId5 = 905132; //風
	private static final int _bossId6 = 905125; //地
	private static final int _bossId7 = 905126; //火
	private static final int _bossId8 = 905127; //水
	private static final int _bossId9 = 905128; //風
	
	public static int[] bossDeathCount= new int[6];

	public String enterTakiCaveDragon(L1PcInstance pc, L1ItemInstance item) throws Exception {
		L1PcInstance leader = pc.getParty().getLeader();
		L1PcInstance member[] = pc.getParty().getMembers();
		short mapId = 7300;
		int locX = 32724;
		int locY = 32849;
		mapId = (short) (7300 + getMembersCount());

		if (getMembersCount() >= 6) {
			pc.sendPackets(new S_SystemMessage("副本已滿。"));
			return "";
		}
		for (int i = 0, a = member.length; i < a; i++) {
			if (member[i].getId() == leader.getId() || member[i] == null)
				continue;
			L1Teleport.teleport(leader, locX, locY, mapId, pc.getHeading(), true);
			pc.setDailyMapID(mapId);
			pc.save();
			pc.getInventory().removeItem(item, 1);
			addMembers(pc);
			switch (member.length){
			case 8:
				if (member[7].getDailyCave() >= 1){
					L1Teleport.teleport(member[7], locX, locY, mapId, pc.getHeading(), true);
					member[7].setDailyMapID(mapId);
					member[7].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[7].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[7].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
			case 7:
				if (member[6].getDailyCave() >= 1){
					L1Teleport.teleport(member[6], locX, locY, mapId, pc.getHeading(), true);
					member[6].setDailyMapID(mapId);
					member[6].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[6].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[6].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
			case 6:
				member[5].save();
				if (member[5].getDailyCave() >= 1){
					L1Teleport.teleport(member[5], locX, locY, mapId, pc.getHeading(), true);
					member[5].setDailyMapID(mapId);
					member[5].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[5].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[5].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
			case 5:
				if (member[4].getDailyCave() >= 1){
					L1Teleport.teleport(member[4], locX, locY, mapId, pc.getHeading(), true);
					member[4].setDailyMapID(mapId);
					member[4].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[4].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[4].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
			case 4:				
				if (member[3].getDailyCave() >= 1){
					L1Teleport.teleport(member[3], locX, locY, mapId, pc.getHeading(), true);
					member[3].setDailyMapID(mapId);
					member[3].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[3].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[3].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
			case 3:
				if (member[2].getDailyCave() >= 1){
					L1Teleport.teleport(member[2], locX, locY, mapId, pc.getHeading(), true);
					member[2].setDailyMapID(mapId);
					member[2].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[2].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[2].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
			case 2:
				if (member[1].getDailyCave() >= 1){
					L1Teleport.teleport(member[1], locX, locY, mapId, pc.getHeading(), true);
					member[1].setDailyMapID(mapId);
					member[1].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[1].getName()+"今天還沒完成炎魔副本，不能進入巨龍副本。"));
					member[1].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本，不能進入巨龍副本。。"));
				}
				break;
			}
			return "";
		}
		return "";
	}
	
	private void addMembers(L1PcInstance pc) {
		_members.add(pc); 
		GeneralThreadPool.getInstance().execute(new runDragonCave());
	}	

	private class runDragonCave implements Runnable {
		
		public void run() {
			int rnd1 = _random.nextInt(4);

			int _bossId1 = _ramdrgon[rnd1];
	
			try {
				int mapid = 7299 + getMembersCount();
				
				sendMessage(mapid,"【巫女】莎爾：這空間的能量異常強烈，連接的通道我只再能支撐一刻鐘");
				Thread.sleep(_speaktime);
				
				sendMessage(mapid,"【屠龍者】卡瑞：勇士們，握好手中的武器，準備擊殺巨龍");
				Thread.sleep(_speaktime);
				
				sendMessage(mapid,"副本將於30秒後開始");
				Thread.sleep(_readytime);
	
				sendMessage(mapid,"倒數10秒");
				Thread.sleep(10*1000);
	
				sendMessage(mapid,"5秒");
				Thread.sleep(1000);
	
				sendMessage(mapid,"4秒");
				Thread.sleep(1000);
	
				sendMessage(mapid,"3秒");
				Thread.sleep(1000);
	
				sendMessage(mapid,"2秒");
				Thread.sleep(1000);
	
				sendMessage(mapid,"1秒");
				Thread.sleep(1000);
				
				if (checkParty(mapid)){
					Thread.sleep(1000);
					
					sendMessage(mapid,"那裡來的螞蟻，打擾我的午睡時間");
					Thread.sleep(_speaktime);
					
					spawn(_bossId1,mapid);
					Thread.sleep(_readytime);
					
					sendMessage(mapid,"龍族！出來清理迷路的螞蟻");
					Thread.sleep(_speaktime);
					
					switch (_bossId1){
					case 905121:
						spawn1(_bossId5, mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"林德拜爾：殺光它們!");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId5,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"林德拜爾：你把我激怒了!");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId9,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"林德拜爾：．．．．．．．");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId9,mapid);
						Thread.sleep(_bosstime);
						break;
					case 905122:
						spawn1(_bossId2,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"安塔瑞斯：你們以為我是誰？我乃龍族的安塔瑞斯！");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId2,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"安塔瑞斯：吃光你們也不能消除我的怒氣！");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId6,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"安塔瑞斯：你會後悔的．．．．");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId6,mapid);
						Thread.sleep(_bosstime);
						break;
					case 905123:
						spawn1(_bossId4,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"法利昂：孩子們，給我上！");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId4,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"法利昂：哈哈哈．．還能繼續嗎？");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId8,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"法利昂：我是不會敗的！絕對．．．");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId8,mapid);
						Thread.sleep(_bosstime);
						break;
					case 905124:
						spawn1(_bossId3,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"巴拉卡斯：愚蠢的蟻民，當孩子們的午餐也不夠。");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId3,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"巴拉卡斯：你們必定會付出代價！");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId7,mapid);
						Thread.sleep(_bosstime);
						
						sendMessage(mapid,"巴拉卡斯：吼！");
						Thread.sleep(_speaktime);
						
						spawn1(_bossId7,mapid);
						Thread.sleep(_bosstime);
						break;
					}

					sendMessage(mapid,"【巫女】莎爾：我已經撐不住了！");
					Thread.sleep(_speaktime);
					
					sendMessage(mapid,"【屠龍者】卡瑞：勇士們，先撤退！");
					Thread.sleep(_speaktime);
					
					sendMessage(mapid,"倒數10秒副本結束");
					Thread.sleep(10*1000);
		
					sendMessage(mapid,"5秒");
					Thread.sleep(1000);
		
					sendMessage(mapid,"4秒");
					Thread.sleep(1000);
		
					sendMessage(mapid,"3秒");
					Thread.sleep(1000);
		
					sendMessage(mapid,"2秒");
					Thread.sleep(1000);
		
					sendMessage(mapid,"1秒");
					Thread.sleep(1000);
	
					endDragonCave(mapid);
				}
	
				Thread.sleep(_cleartime);	
	
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	
		}
	}

	private boolean checkParty(int mapid) {
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (!pc.isInParty()){
				if (pc.getMapId() == mapid && pc.getDailyMapID() == mapid){
					sendMessage(mapid,"檢察到玩家沒有組隊，副本強制結束");
					L1Teleport.teleport(pc, 33442, 32797, (short) 4, pc.getHeading(), true);
					endDragonCave(mapid);
					return false;
				}
			}
		}
		return true;
	}
	
	private void endDragonCave(int mapid) {
		switch (mapid){
		case 7305:
			clearMembers();
		default:
			sendMessage(mapid,"副本結束");
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId() == mapid){
						L1Teleport.teleport(pc, 33442, 32797, (short) 4, pc.getHeading(), true);
					}
				}
			clearColosseum(mapid);
			break;
		}
	}	
	
	private void clearColosseum(int mapid) {
		for (Object obj : L1World.getInstance().getVisibleObjects(mapid).values()) {
			if (obj instanceof L1MonsterInstance) {
				L1MonsterInstance mob = (L1MonsterInstance) obj;
				if (!mob.isDead()) {
					mob.setDead(true);
					mob.setStatus(ActionCodes.ACTION_Die);
					mob.setCurrentHpDirect(0);
					mob.deleteMe();
				}
			} else if (obj instanceof L1Inventory) {
				L1Inventory inventory = (L1Inventory) obj;
				inventory.clearItems();
			}
		}
	}
	
	private void spawn(int npcid, int mapid) {
		try {
			L1NpcInstance npc = NpcTable.getInstance().newNpcInstance(npcid);
			npc.setId(IdFactory.getInstance().nextId());
			npc.setMap((short) mapid);
			npc.setX(32706);
			npc.setY(32827);
			Thread.sleep(1);
			npc.setHomeX(npc.getX());
			npc.setHomeY(npc.getY());
			npc.setHeading(6);
	
			L1World.getInstance().storeObject(npc);
			L1World.getInstance().addVisibleObject(npc);
			npc.turnOnOffLight();
			npc.startChat(L1NpcInstance.CHAT_TIMING_APPEARANCE);
	
		} catch (Exception e) {
		}
	}
	
	private void spawn1(int npcid, int mapid) {
		int _loc[][] = {{32706,32823}, {32706,32819}, {32710,32827},
				{32714,32827}, {32702,32827}, {32698,32827},
				{32706,32835}, {32684,32810}};
		for (int i=0; i<8; i++){
			try {
				L1NpcInstance npc;
				npc = new L1NpcInstance(null);
				npc = NpcTable.getInstance().newNpcInstance(npcid);
				npc.setId(IdFactory.getInstance().nextId());
				npc.setMap((short) mapid);
				npc.setX(_loc[i][0]);
				npc.setY(_loc[i][1]);
				Thread.sleep(1);
				npc.setHomeX(npc.getX());
				npc.setHomeY(npc.getY());
				npc.setHeading(4);
				
				L1World.getInstance().storeObject(npc);
				L1World.getInstance().addVisibleObject(npc);
				npc.turnOnOffLight();
				npc.startChat(L1NpcInstance.CHAT_TIMING_APPEARANCE);
				
			} catch (Exception e) {
			}
		}
	}
	
	private void sendMessage(int mapid, String msg) {
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (pc.getMapId() == mapid){
				pc.sendPackets(new S_BlueMessage(166,"\\f3" + msg));
			}
		}
	}	

	private void clearMembers() {
		_members.clear();
	}
	
	public int getMembersCount() {
		return _members.size();
	}
}